package com.dameng.jianyouquan.jobhunter.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.jobhunter.fragment.home.LocalFragment;
import com.dameng.jianyouquan.jobhunter.fragment.home.LongPeriodFragment;
import com.dameng.jianyouquan.jobhunter.fragment.home.ShortPeriodFragment;

public class HomeFragment extends Fragment {

    //声明AMapLocationClientOption对象
    private AMapLocationClientOption mLocationOption = null;
    private AMapLocationClient mLocationClient = null;
    private String city;
    private ViewPager mViewPage;
    private TextView mTvlocation;
    private RelativeLayout mRllocation;
    private RelativeLayout mRlshortPeriod;
    private RelativeLayout mRllongPeriod;
    private int selectItem = 1;

    //声明定位回调监听器
    private AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            String aMapCity = aMapLocation.getCity();
            if (!TextUtils.isEmpty(aMapCity)) {
                mLocationClient.stopLocation();
                if (aMapCity.contains("市")){
                    aMapCity=aMapCity.replace("市","");
                }
                city = aMapCity;
                latitude = aMapLocation.getLatitude();
                longitude = aMapLocation.getLongitude();
                if (mTvlocation != null) {
                    mTvlocation.setText(city);
                }
            }
        }
    };
    private TextView mTvShortPeroid;
    private TextView mTvLongPeroid;
    private double latitude;
    private double longitude;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化定位
        mLocationClient = new AMapLocationClient(getContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        AMapLocationClientOption option = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        if (null != mLocationClient) {
            mLocationClient.setLocationOption(option);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.startLocation();
        }

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getContext(), R.layout.fragment_home, null);
        mTvlocation = view.findViewById(R.id.tv_location);
        mRllocation = view.findViewById(R.id.rl_location);
        mRlshortPeriod = view.findViewById(R.id.rl_short_period);
        mRllongPeriod = view.findViewById(R.id.rl_long_period);
        mTvShortPeroid = view.findViewById(R.id.tv_short_period);
        mTvLongPeroid = view.findViewById(R.id.tv_long_period);
        mViewPage = view.findViewById(R.id.vp);
        mViewPage.setAdapter(new HomeAdapter(getChildFragmentManager()));

        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, ScaleAnimation.RELATIVE_TO_SELF,
                0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(10);
        scaleAnimation.setFillAfter(true);
        mTvlocation.startAnimation(scaleAnimation);


        mRllocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectItem == 1) {
                    return;
                }
                bigger(mTvlocation);
                if (selectItem == 2) {
                    smaller(mTvShortPeroid);
                } else if (selectItem == 3) {
                    smaller(mTvLongPeroid);
                }
                selectItem = 1;
                mViewPage.setCurrentItem(0);
            }
        });

        mRlshortPeriod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectItem == 2) {
                    return;
                }
                bigger(mTvShortPeroid);
                if (selectItem == 1) {
                    smaller(mTvlocation);
                } else if (selectItem == 3) {
                    smaller(mTvLongPeroid);
                }
                selectItem = 2;
                mViewPage.setCurrentItem(1);
            }
        });

        mRllongPeriod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectItem == 3) {
                    return;
                }
                bigger(mTvLongPeroid);
                if (selectItem == 1) {
                    smaller(mTvlocation);
                } else if (selectItem == 2) {
                    smaller(mTvShortPeroid);
                }
                selectItem = 3;
                mViewPage.setCurrentItem(2);
            }
        });
        return view;
    }

    private class HomeAdapter extends FragmentPagerAdapter {
        private HomeAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                LocalFragment fragment=new LocalFragment();
//                Bundle bundle=new Bundle();
//                bundle.putString("title",title);
//                fragment.setArguments(bundle);
//                //即使fragment重构，也会通过反射的机制，把重新实例化的fragment设上参数title
                return fragment;
            } else if (position == 1) {
                return new LongPeriodFragment();
            } else if (position == 2) {
                return new ShortPeriodFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }


    private void bigger(View view) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, ScaleAnimation.RELATIVE_TO_SELF,
                0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(300);
        scaleAnimation.setFillAfter(true);
        view.startAnimation(scaleAnimation);
    }


    private void smaller(View view) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.5f, 1f, 1.5f, 1f, ScaleAnimation.RELATIVE_TO_SELF,
                0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(300);
        scaleAnimation.setFillAfter(true);
        view.startAnimation(scaleAnimation);
    }
}
